package fm.dongman.animefm.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.modellib.model.SheetModel;

/**
 * Created by alex on 2017/11/3.
 *
 * @description 漫单选择对话框
 */

public class ChooseSheetDialog extends Dialog {

    private ChooseSheetDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context mContext;
        private List<SheetModel> mSheetModels;
        private ChooseSheetListener mChooseSheetListener;
        private ChooseSheetDialog sheetdialog;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setSheetList(@NotNull List<SheetModel> sheets) {
            mSheetModels = sheets;
            return this;
        }

        public Builder setChooseSheetListener(ChooseSheetListener listener) {
            mChooseSheetListener = listener;
            return this;
        }

        public ChooseSheetDialog build() {
            sheetdialog = new ChooseSheetDialog(mContext, R.style.CustomDialog);
            View layoutView = View.inflate(mContext, R.layout.dialog_choose_sheet, null);
            Button btnCreateSheet = (Button) layoutView.findViewById(R.id.mBtnDialogCreateDialog);
            btnCreateSheet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mChooseSheetListener.onCreateNewSheet(sheetdialog);
                }
            });
            RecyclerView rvSheet = (RecyclerView) layoutView.findViewById(R.id.mRvDialogChooseSheet);
            rvSheet.setAdapter(new SheetAdapter());
            rvSheet.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
            sheetdialog.setContentView(layoutView);
            return sheetdialog;
        }

        public interface ChooseSheetListener {
            void onCreateNewSheet(Dialog dialog);

            void onSheetItemClick(String sheetId, Dialog dialog);
        }

        private class SheetAdapter extends RecyclerView.Adapter<SheetAdapter.ViewHolder> {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = View.inflate(mContext, R.layout.item_dialog_choose_sheet, null);
                return new ViewHolder(v);
            }

            @Override
            public void onBindViewHolder(ViewHolder holder, final int position) {
                final SheetModel sheet = mSheetModels.get(position);
                holder.sheetTitle.setText(sheet.getMandanTitle());
                Glide.with(mContext).load(sheet.getMandanImg()).into(holder.sheetImg);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mChooseSheetListener != null)
                            mChooseSheetListener.onSheetItemClick(sheet.getMandanId(), sheetdialog);
                    }
                });
            }

            @Override
            public int getItemCount() {
                return mSheetModels.size();
            }

            public class ViewHolder extends RecyclerView.ViewHolder {
                ImageView sheetImg;
                TextView sheetTitle;

                public ViewHolder(View itemView) {
                    super(itemView);
                    sheetImg = (ImageView) itemView.findViewById(R.id.mIvDialogSheetImg);
                    sheetTitle = (TextView) itemView.findViewById(R.id.mTvDialogSheetTitle);
                }
            }
        }
    }


}

